<?php
namespace Admin\Controller;
use Think\Controller;
class XmController extends CommonController {
    public function index()
    {
        $count = M('gy_xm')->count();
        $this->assign('count',$count);

    	$xms = M('gy_xm')->select();
    	$this->assign('xms',$xms);

        $this->display();
    }

    //上线 下线
    public function online()
    {
        if (IS_POST) {
            $id = I('post.id');

            $uid = M('gy_xm')->where(['id'=>$id])->getField('uid');
            $is_rz = M('gy_rz_ger')->where(['uid'=>$uid])->getField('is_rz');
            if ($is_rz != 1) {
                $arr = array('error' => 0 ,'info' => '请根据用户名到 (用户管理-用户列表-认证表) 进行确认');
                echo json_encode($arr);
                exit;
            }

            $res = M('gy_xm')->where(['id'=>$id])->setField('is_online',1);
            if ($res) {
                $arr = array('error' => 1 ,'info' => '上线成功');
                echo json_encode($arr);
            }else {
                $arr = array('error' => 0 ,'info' => '上线失败');
                echo json_encode($arr);
            }
        }
    }
    public function nonline()
    {
        if (IS_POST) {
            $id = I('post.id');
            $res = M('gy_xm')->where(['id'=>$id])->setField('is_online',0);
            if ($res) {
                $arr = array('error' => 1 ,'info' => '下线成功');
                echo json_encode($arr);
            }else {
                $arr = array('error' => 0 ,'info' => '下线失败');
                echo json_encode($arr);
            }
        }
    }
    //推荐 下推荐
    public function tuij()
    {
        if (IS_POST) {
            $id = I('post.id');
            $res = M('gy_xm')->where(['id'=>$id])->setField('is_tj',1);
            if ($res) {
                $arr = array('error' => 1 ,'info' => '推荐成功');
                echo json_encode($arr);
            }else {
                $arr = array('error' => 0 ,'info' => '推荐失败');
                echo json_encode($arr);
            }
        }
    }
    public function ntuij()
    {
        if (IS_POST) {
            $id = I('post.id');
            $res = M('gy_xm')->where(['id'=>$id])->setField('is_tj',0);
            if ($res) {
                $arr = array('error' => 1 ,'info' => '取消推荐');
                echo json_encode($arr);
            }else {
                $arr = array('error' => 0 ,'info' => '取消失败');
                echo json_encode($arr);
            }
        }
    }

    //结束 no
    public function over()
    {
        if (IS_POST) {
            $id = I('post.id');
            $res = M('gy_xm')->where(['id'=>$id])->setField('is_over',0);
            if ($res) {
                $arr = array('error' => 1 ,'info' => '项目结束');
                echo json_encode($arr);
            }else {
                $arr = array('error' => 0 ,'info' => '结束失败');
                echo json_encode($arr);
            }
        }
    }
    public function nover()
    {
        if (IS_POST) {
            $id = I('post.id');
            $res = M('gy_xm')->where(['id'=>$id])->setField('is_over',1);
            if ($res) {
                $arr = array('error' => 1 ,'info' => '项目开始');
                echo json_encode($arr);
            }else {
                $arr = array('error' => 0 ,'info' => '开始失败');
                echo json_encode($arr);
            }
        }
    }

    //edit
    public function edit()
    {
    	if (IS_POST) {
    		$id = I('post.id');

            $data['xm_name'] = trim(I('post.xm_name'));
            $data['xm_days'] = I('post.xm_days');

            $data['xm_type'] = I('post.xm_type');
            $data['xm_diyu'] = I('post.xm_diyu');

            $data['xm_mubiao'] = I('post.xm_mubiao');
            $data['organ'] = I('post.organ');

    		$data['xm_content'] = I('post.xm_content');
            $data['xm_xiangq'] = I('post.xm_xiangq');
            $data['xm_list'] = I('post.xm_list');

            if (empty($data['xm_name'])) {
                $arr = array(
                    'error' => 0,
                    'info' => '项目名称不能为空'
                    );
                echo json_encode($arr);
                exit;
            }

            if (!ctype_digit($data['xm_days'])) {
                $arr = array(
                    'error' => 0,
                    'info' => '项目天数为整数'
                    );
                echo json_encode($arr);
                exit;
            }
            if (empty($data['xm_type'])) {
                $arr = array(
                    'error' => 0,
                    'info' => '项目类型不能为空'
                    );
                echo json_encode($arr);
                exit;
            }
            if (empty($data['xm_diyu'])) {
                $arr = array(
                    'error' => 0,
                    'info' => '项目地域不能为空'
                    );
                echo json_encode($arr);
                exit;
            }
            //检测目标是否为整数
            if (!ctype_digit($data['xm_mubiao'])) {
                $arr = array(
                    'error' => 0,
                    'info' => '项目目标为整数'
                );
                echo json_encode($arr);
                exit;
            }
            if (empty($data['organ'])) {
                $arr = array('error' => 0,'info' => '接收组织不能为空');echo json_encode($arr);exit;
            }
            if (empty($data['xm_content'])) {
                $arr = array('error' => 0,'info' => '项目内容不能为空');echo json_encode($arr);exit;
            }
            if (empty($data['xm_xiangq'])) {
                $arr = array('error' => 0,'info' => '项目详情不能为空');echo json_encode($arr);exit;
            }
            if (empty($data['xm_list'])) {
                $arr = array('error' => 0,'info' => '筹集清单不能为空');echo json_encode($arr);exit;
            }

            $res = M('gy_xm')->where(['id'=>$id])->save($data);
            if ($res) {
                $arr = array(
                    'error' => 1,
                    'info' => '更新成功'
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '更新失败'
                    );
                echo json_encode($arr);
            }
    	}else {
	    	$id = I('get.id');
	    	$xm_info = M('gy_xm')->where(['id'=>$id])->find();
	    	$this->assign('xm_info',$xm_info);

	    	$this->display();
    	}
    }

    //删除
    public function delete()
    {
        if (IS_POST) {
            $id = I('post.id');
            $res = M('gy_xm')->delete($id);
            if ($res) {
                $arr = array(
                    'error' => 1 ,
                    'info' => '删除成功'
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0 ,
                    'info' => '删除失败'
                    );
                echo json_encode($arr);
            }
        }
    }




}